package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

import myz.date.gui.NumberLenghtLimitedDmt;
import db.domain.Student;
import db.sqldao.StudentSqlDao;

/**
 * 学号输入界面
 * 
 * @author 陈千琪
 * @date 2011-06-26
 * @version 1.0.0.0
 * 
 */

@SuppressWarnings("serial")
public class StudentInputForm extends JDialog implements ActionListener {
	private JLabel lbLTitle;
	private JTextField txtNo;
	private JButton btnNext;
	private String type;

	public StudentInputForm(String type) {
		this.type = type;
		this.setTitle("学号输入界面");
		this.setLayout(null);
		lbLTitle = new JLabel("请输入学号");
		Font font = new Font("黑体", Font.BOLD, 30);
		lbLTitle.setFont(font);
		lbLTitle.setBounds(10, 10, 200, 30);
		lbLTitle.setForeground(Color.blue);
		this.add(lbLTitle);
		txtNo = new JTextField();
		txtNo.setHorizontalAlignment(JTextField.CENTER);
		txtNo.setForeground(Color.red);
		Font font1 = new Font("黑体", Font.BOLD, 20);
		txtNo.setFont(font1);
		txtNo.setBounds(10, 50, 200, 30);
		txtNo.setBorder(new LineBorder(Color.black, 2, false));
		txtNo.setDocument(new NumberLenghtLimitedDmt(5));
		this.add(txtNo);

		btnNext = new JButton("下一步");
		btnNext.addActionListener(this);
		btnNext.setFont(font1);
		btnNext.setBounds(60, 90, 100, 30);
		this.add(btnNext);
		this.setModal(true);// 窗口独占模式
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		// 匿名内部类
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent arg0) {
				dispose();
			}
		});
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = new Dimension(230, 160);
		if (frameSize.height > screenSize.height) {
			frameSize.height = screenSize.height;
		}
		if (frameSize.width > screenSize.width) {
			frameSize.width = screenSize.width;
		}
		setLocation(((screenSize.width - frameSize.width) / 2),
				((screenSize.height - frameSize.height) / 2));
		// 设置窗口的大小
		setSize(frameSize);
		this.setResizable(false);
		this.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnNext) { // 下一步
			// (1)判断学号不能为空
			String no = txtNo.getText().trim();
			if (no.length() == 0) {
				JOptionPane.showMessageDialog(this, "对不起，请重新输入学号！");
				txtNo.requestFocus(true);
				return;
			}
			// (2)判断学号是否存在
			StudentSqlDao sDao = new StudentSqlDao();
			ArrayList<Student> al = sDao.getStudent("where no=" + no);// binary是二进制转换用来区分大小写
			if (al.size() == 0) {
				JOptionPane.showMessageDialog(this, "对不起，【学号】不存在，请重新输入！");
				// 文本框中的内容自动选中
				txtNo.setSelectionStart(0);
				txtNo.setSelectionEnd(txtNo.getText().length());
				txtNo.requestFocus(true);
				return;
			}
			// 存在，下一个界面
			this.dispose();
			if ("SetAdd".equals(type)) {
				new AddMoney(al.get(0));
			} else {
				new StudentManagerForm(type, al.get(0));
			}

		}
	}
}
